package com.saydc.actions.capacitaciones;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class DonwloadListadoCapacitacionesAction extends Action {
	
	private static final String FORMULARIOS_PATH = "C:/Users/Mariano/workspace/SAYDC/resources/com/saydc/files/listados/";
	private static final String FILE_NAME = "SAyDC-Capacitaciones V1.0";
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		InputStream in = null;
		try{  
			in = new FileInputStream(new File(FORMULARIOS_PATH+FILE_NAME+".pdf"));
			response.setContentType("application/pdf");
			response.setHeader("Content-disposition","attachment;filename=ListadoCapacitacionesPDF.pdf");
			int i;
			while ((i=in.read())!=-1){
		      response.getOutputStream().write(i);
			}
		}catch(FileNotFoundException e){
			return mapping.findForward("fileFailure");
		}catch(Exception exception){
			return mapping.findForward("fileFailure");
		}finally{  
			if(in != null){
				try {
					response.getOutputStream().close();
					in.close (  ) ;
				} catch (IOException e) {
					return mapping.findForward("fileFailure");
				}
			}
		}
		return mapping.findForward("success");
	}

}
